#!/usr/bin/env python

from social_class import Social

def read_config ( liwe ):
	"""
	:INPUT:
		none
	"""
	s = Social ( liwe )

	settings = {
		'facebook' : {
			'appid': s.cfg.get ( 'facebook.app_id' ),
			'redirect': s.cfg.get ( 'facebook.redirect' )
		}
	}

	return { 'settings': settings }

def return_token ( liwe ):
	"""
	:INPUT:
		none
	"""
	s = Social ( liwe )

	return { '_no_json': True, '_text': s.templates [ 'facebook-return-token' ] }

# {{{ public methods (invoke)
def post ( liwe, action_name, ref_id, message = '', picture = '', link = '', name = '', caption = '', description = '' ):
	"""
	:INPUT:
		- action_name:		SAFE_STRING
		- ref_id:		SAFE_STRING
		- message:		SAFE_STRING
		- picture:		SAFE_STRING
		- link:			SAFE_STRING
		- name:			SAFE_STRING
		- caption:		SAFE_STRING
		- description:		SAFE_STRING
	"""
	s = Social ( liwe )

	res = s.post ( action_name, ref_id, message, picture, link, name, caption, description )

	if res:
		if res.has_key ( 'id' ):
			return { 'status': 'OK', 'post_id': res [ 'id' ] }
		else:
			return { 'status': 'Error', 'message': res [ 'error' ] [ 'message' ] }
	else:
		return { 'status': 'Errore sconosciuto nel post' }

def post_list ( liwe, action_name, ref_id = '' ):
	"""
	:INPUT:
		- action_name:		SAFE_STRING
		- ref_id:		SAFE_STRING
	"""
	s = Social ( liwe )

	return { 'list' : s.post_list ( action_name, ref_id ) }

# }}}

# {{{ facebook
def facebook_post ( liwe, page_id = '', impersonate = '', text = '' ):
	"""
	:INPUT:
		- page_id:		SAFE_STRING
		- impersonate:		SAFE_STRING
		- text:			SAFE_STRING
	"""
	s = Social ( liwe )

	return { 'res' : s.facebook_post ( page_id, impersonate, { 'message' : text } ) }

def facebook_page_list ( liwe ):
	"""
	:INPUT:
		none
	"""
	s = Social ( liwe )

	return { 'list' : s.facebook_page_list () }
# }}}

# {{{ token
def token_list ( liwe ):
	"""
	:INPUT:
		none
	"""
	s = Social ( liwe )
	
	tokens = s.token_list ()
	if tokens:
		for row in tokens:
			row [ 'token' ] = "%s...%s" % ( row [ 'token' ] [ : 8 ], row [ 'token' ] [ -8 : ] )

	return { 'list': tokens }

def token_save ( liwe, service, access_token ):
	"""
	:INPUT:
		- service:	SAFE_STRING
		- access_token:	SAFE_STRING
	"""
	s = Social ( liwe )

	s.token_save ( service, access_token )

	return { '_no_json': True, '_text': '<script>window.close()</script>' }

def token_delete ( liwe, uid, service = '' ):
	"""
	:INPUT:
		- uid:		SAFE_STRING
		- service:	SAFE_STRING
	"""
	s = Social ( liwe )

	s.token_delete ( uid, service )

	return { 'status' : 'OK' }
# }}}

# {{{ page
def page_list ( liwe ):
	"""
	:INPUT:
		none
	"""
	s = Social ( liwe )

	return { 'list' : s.page_list () }

def page_save ( liwe, service, page_id, label ):
	"""
	:INPUT:
		- service:	SAFE_STRING
		- page_id:	SAFE_STRING
		- label:	SAFE_STRING
	"""
	s = Social ( liwe )

	res = s.page_save ( service, page_id, label )

	if res:
		return { 'status': 'OK' }
	else:
		return { 'status': 'Pagina esistente' }

def page_delete ( liwe, page_id, service ):
	"""
	:INPUT:
		- page_id:	SAFE_STRING
		- service:	SAFE_STRING
	"""
	s = Social ( liwe )

	s.page_delete ( page_id, service )

	return { 'status': 'OK' }
# }}}

# {{{ action
def action_list ( liwe ):
	"""
	:INPUT:
		none
	"""
	s = Social ( liwe )

	return { 'list' : s.action_list () }

def action_save ( liwe, name, service, page_id ):
	"""
	:INPUT:
		- name:		SAFE_STRING
		- service:	SAFE_STRING
		- page_id:	SAFE_STRING
	"""
	s = Social ( liwe )

	res = s.action_save ( name, service, page_id )

	if res:
		return { 'status': 'OK' }
	else:
		return { 'status': 'Azione esistente' }

def action_delete ( liwe, name ):
	"""
	:INPUT:
		- name:		SAFE_STRING
	"""
	s = Social ( liwe )

	s.action_delete ( name )

	return { 'status': 'OK' }
# }}}
